home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / modes / make-mode.el.z / make-mode.el
Encoding:
Text File  |  1998-05-21  |  50.2 KB  |  1,486 lines

  1. ;;; make-mode.el --- makefile editing commands for Emacs
  2.  
  3. ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Thomas Neumann <tom@smart.bo.open.de>
  6. ;;    Eric S. Raymond <esr@snark.thyrsus.com>
  7. ;; Adapted-By: ESR
  8. ;; Keywords: unix, tools
  9.  
  10. ;; RMS:
  11. ;; This needs work.
  12. ;; Also, the doc strings need fixing: the first line doesn't stand alone,
  13. ;; and other usage is not high quality.  Symbol names don't have `...'.
  14.  
  15. ;; This file is part of XEmacs.
  16.  
  17. ;; XEmacs is free software; you can redistribute it and/or modify it
  18. ;; under the terms of the GNU General Public License as published by
  19. ;; the Free Software Foundation; either version 2, or (at your option)
  20. ;; any later version.
  21.  
  22. ;; XEmacs is distributed in the hope that it will be useful, but
  23. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  24. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  25. ;; General Public License for more details.
  26.  
  27. ;; You should have received a copy of the GNU General Public License
  28. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  29. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  30. ;; 02111-1307, USA.
  31.  
  32. ;;; Synched up with: FSF 19.34.
  33.  
  34. ;;; Commentary:
  35.  
  36. ;; A major mode for editing makefiles.  The mode knows about Makefile
  37. ;; syntax and defines M-n and M-p to move to next and previous productions.
  38. ;;
  39. ;; The keys $, =, : and . are electric; they try to help you fill in a
  40. ;; macro reference, macro definition, ordinary target name, or special
  41. ;; target name, respectively.  Such names are completed using a list of
  42. ;; targets and macro names parsed out of the makefile.  This list is
  43. ;; automatically updated, if necessary, whenever you invoke one of
  44. ;; these commands.  You can force it to be updated with C-c C-p.
  45. ;;
  46. ;; The command C-c C-f adds certain filenames in the current directory
  47. ;; as targets.  You can filter out filenames by setting the variable
  48. ;; makefile-ignored-files-in-pickup-regex.
  49. ;;
  50. ;; The command C-c C-u grinds for a bit, then pops up a report buffer
  51. ;; showing which target names are up-to-date with respect to their
  52. ;; prerequisites, which targets are out-of-date, and which have no
  53. ;; prerequisites.
  54. ;;
  55. ;; The command C-c C-b pops up a browser window listing all target and
  56. ;; macro names.  You can mark or unmark items wit C-c SPC, and insert
  57. ;; all marked items back in the Makefile with C-c TAB.
  58. ;;
  59. ;; The command C-c TAB in the makefile buffer inserts a GNU make builtin.
  60. ;; You will be prompted for the builtin's args.
  61. ;;
  62. ;; There are numerous other customization variables.
  63.  
  64. ;;
  65. ;; To Do:
  66. ;;
  67. ;; * makefile-backslash-region should be given better behavior.
  68. ;; * Consider binding C-c C-c to comment-region (like cc-mode).
  69. ;; * Eliminate electric stuff entirely.
  70. ;; * It might be nice to highlight targets differently depending on
  71. ;;   whether they are up-to-date or not.  Not sure how this would
  72. ;;   interact with font-lock.
  73. ;; * Would be nice to edit the commands in ksh-mode and have
  74. ;;   indentation and slashification done automatically.  Hard.
  75. ;; * Consider removing browser mode.  It seems useless.
  76. ;; * ":" should notice when a new target is made and add it to the
  77. ;;   list (or at least set makefile-need-target-pickup).
  78. ;; * Make browser into a major mode.
  79. ;; * Clean up macro insertion stuff.  It is a mess.
  80. ;; * Browser entry and exit is weird.  Normalize.
  81. ;; * Browser needs to be rewritten.  Right now it is kind of a crock.
  82. ;;   Should at least:
  83. ;;    * Act more like dired/buffer menu/whatever.
  84. ;;    * Highlight as mouse traverses.
  85. ;;    * B2 inserts.
  86. ;; * Update documentation above.
  87. ;; * Update texinfo manual.
  88. ;; * Update files.el.
  89.  
  90.  
  91.  
  92. ;;; Code:
  93.  
  94. (provide 'makefile)
  95.  
  96. (defgroup makefile-mode nil
  97.   "Makefile mode customizations"
  98.   :group 'tools
  99.   :prefix "makefile-")
  100.  
  101.  
  102. ;; Sadly we need this for a macro.
  103. (eval-when-compile
  104.   (unless (featurep 'xemacs)
  105.     (require 'imenu)))
  106.  
  107. ;;; ------------------------------------------------------------
  108. ;;; Configurable stuff
  109. ;;; ------------------------------------------------------------
  110.  
  111. (defcustom makefile-browser-buffer-name "*Macros and Targets*"
  112.   "Name of the macro- and target browser buffer."
  113.   :type 'string
  114.   :group 'makefile-mode)
  115.  
  116. (defcustom makefile-target-colon ":"
  117.   "String to append to all target names inserted by `makefile-insert-target'.
  118. \":\" or \"::\" are common values."
  119.   :type 'string
  120.   :group 'makefile-mode)
  121.  
  122. (defcustom makefile-macro-assign " = "
  123.   "String to append to all macro names inserted by `makefile-insert-macro'.
  124. The normal value should be \" = \", since this is what
  125. standard make expects. However, newer makes such as dmake
  126. allow a larger variety of different macro assignments, so you
  127. might prefer to use \" += \" or \" := \" ."
  128.   :type 'string
  129.   :group 'makefile-mode)
  130.  
  131. (defcustom makefile-electric-keys nil
  132.   "If non-nil, install electric keybindings.
  133. Default is nil."
  134.   :type 'boolean
  135.   :group 'makefile-mode)
  136.  
  137. (defcustom makefile-use-curly-braces-for-macros-p nil
  138.   "Controls the style of generated macro references.
  139. t (actually non-nil) means macro references should use curly braces,
  140. like `${this}'.
  141. nil means use parentheses, like `$(this)'."
  142.   :type 'boolean
  143.   :group 'makefile-mode)
  144.  
  145. (defcustom makefile-tab-after-target-colon t
  146.   "If non-nil, insert a TAB after a target colon.
  147. Otherwise, a space is inserted.
  148. The default is t."
  149.   :type 'boolean
  150.   :group 'makefile-mode)
  151.  
  152. (defcustom makefile-browser-leftmost-column 10
  153.   "Number of blanks to the left of the browser selection mark."
  154.   :type 'integer
  155.   :group 'makefile-mode)
  156.  
  157. (defcustom makefile-browser-cursor-column 10
  158.   "Column in which the cursor is positioned when it moves
  159. up or down in the browser."
  160.   :type 'integer
  161.   :group 'makefile-mode)
  162.  
  163. (defcustom makefile-backslash-column 48
  164.   "*Column in which `makefile-backslash-region' inserts backslashes."
  165.   :type 'integer
  166.   :group 'makefile-mode)
  167.  
  168. (defcustom makefile-browser-selected-mark "+  "
  169.   "String used to mark selected entries in the browser."
  170.   :type 'string
  171.   :group 'makefile-mode)
  172.  
  173. (defcustom makefile-browser-unselected-mark "   "
  174.   "String used to mark unselected entries in the browser."
  175.   :type 'string
  176.   :group 'makefile-mode)
  177.  
  178. (defcustom makefile-browser-auto-advance-after-selection-p t
  179.   "If non-nil, cursor will move after item is selected in browser."
  180.   :type 'boolean
  181.   :group 'makefile-mode)
  182.  
  183. (defcustom makefile-pickup-everything-picks-up-filenames-p nil
  184.   "If non-nil, `makefile-pickup-everything' picks up filenames as targets.
  185. \(i.e. it calls `makefile-find-filenames-as-targets').
  186. Otherwise filenames are omitted."
  187.   :type 'boolean
  188.   :group 'makefile-mode)
  189.  
  190. (defcustom makefile-cleanup-continuations-p t
  191.   "If non-nil, automatically clean up continuation lines when saving.
  192. A line is cleaned up by removing all whitespace following a trailing
  193. backslash.  This is done silently.
  194. IMPORTANT: Please note that enabling this option causes makefile-mode
  195. to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \'it seems necessary\'."
  196.   :type 'boolean
  197.   :group 'makefile-mode)
  198.  
  199. ;;; those suspicious line warnings are really annoying and
  200. ;;; seem to be generated for every makefile I've ever seen.
  201. ;;; add a simple mechanism to disable them.  -gk
  202. (defcustom makefile-warn-suspicious-lines-p t
  203.   "In non-nil, warn about suspicious lines when saving the makefile"
  204.   :type 'boolean
  205.   :group 'makefile-mode)
  206.  
  207. (defcustom makefile-browser-hook '()
  208.   "The hook to run when entering makefile browser."
  209.   :type 'hook
  210.   :group 'makefile-mode)
  211.  
  212. ;;
  213. ;; Special targets for DMake, Sun's make ...
  214. ;; 
  215. (defvar makefile-special-targets-list
  216.   '(("DEFAULT")      ("DONE")        ("ERROR")        ("EXPORT")
  217.     ("FAILED")       ("GROUPEPILOG") ("GROUPPROLOG")  ("IGNORE")
  218.     ("IMPORT")       ("INCLUDE")     ("INCLUDEDIRS")  ("INIT")
  219.     ("KEEP_STATE")   ("MAKEFILES")   ("MAKE_VERSION") ("NO_PARALLEL")
  220.     ("PARALLEL")     ("PHONY")       ("PRECIOUS")     ("REMOVE")
  221.     ("SCCS_GET")     ("SILENT")      ("SOURCE")       ("SUFFIXES")
  222.     ("WAIT")         ("c.o")         ("C.o")          ("m.o")
  223.     ("el.elc")       ("y.c")         ("s.o"))
  224.   "List of special targets.
  225. You will be offered to complete on one of those in the minibuffer whenever
  226. you enter a \".\" at the beginning of a line in makefile-mode.")
  227.  
  228. (defvar makefile-runtime-macros-list
  229.   '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$"))
  230.   "List of macros that are resolved by make at runtime.
  231. If you insert a macro reference using makefile-insert-macro-ref, the name
  232. of the macro is checked against this list. If it can be found its name will
  233. not be enclosed in { } or ( ).")
  234.  
  235. ;; Note that the first big subexpression is used by font lock.  Note
  236. ;; that if you change this regexp you must fix the imenu index
  237. ;; function defined at the end of the file.
  238. (defconst makefile-dependency-regex
  239.   "^\\([^ \n\t#:]+\\([ \t]+[^ \t\n#:]+\\)*\\)[ \t]*:\\([ \t]*$\\|\\([^=\n].*$\\)\\)" 
  240.   "Regex used to find dependency lines in a makefile.")
  241.  
  242. ;; Note that the first subexpression is used by font lock.  Note that
  243. ;; if you change this regexp you must fix the imenu index function
  244. ;; defined at the end of the file.
  245. (defconst makefile-macroassign-regex
  246.   "^\\([^ \n\t][^:#= \t\n]*\\)[ \t]*[*:+]?:?="
  247.   "Regex used to find macro assignment lines in a makefile.")
  248.  
  249. (defconst makefile-ignored-files-in-pickup-regex
  250.   "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
  251.   "Regex for filenames that will NOT be included in the target list.")
  252.  
  253. ;####
  254. ;(add-to-list 'facemenu-unlisted-faces 'makefile-space-face)
  255. ;  Bogus FSFmacs crap.
  256. (defface makefile-space-face
  257.   '((((class color))
  258.      (:background "hotpink"))        ; Yeah!
  259.     ;; Everything else, just choose the most visible background
  260.     ;; color.  We don't care about foreground, since it is only used
  261.     ;; for whitespace.
  262.     (((background light))
  263.       (:background "black"))
  264.     (((background dark))
  265.       (:background "white")))
  266.   "Face to use for highlighting leading Makefile spaces in Font-Lock mode."
  267.   :group 'makefile-mode)
  268.  
  269. ;Older version of same.
  270. ;(defconst makefile-font-lock-keywords (purecopy
  271. ;  (list
  272. ;   '("^#.*$" . font-lock-comment-face)
  273. ;   '("[^$]#.*$" . font-lock-comment-face)
  274. ;   ;; rules
  275. ;   '("^\\([^ \t\n]*%?[^ \t\n]*[ \t]*::?\\)[ \t]" 1 font-lock-type-face t)
  276. ;   '("^\\(\\.[A-Za-z][A-Za-z]?\\..[ \t]*::?\\)" 1 font-lock-type-face t)
  277. ;   '("^[^ \t\n]+[ \t]*:;?\\(.*\\)$" 1 font-lock-doc-string-face t)
  278. ;   ;; variable definition
  279. ;   '("^[_A-Za-z0-9]+[ \t]*\+?=" . font-lock-function-name-face)
  280. ;   '("\\( \\|:=\\)[_A-Za-z0-9]+[ \t]*\\+=" . font-lock-function-name-face)
  281. ;   ;; variable references
  282. ;   '("\\(\\$\\$?\\([^ \t\n{(]\\|[{(][^ \t\n)}]+[)}]\\)\\)" 
  283. ;     1 font-lock-keyword-face t)
  284. ;   '("^include " . font-lock-string-face)
  285. ;   ))
  286.  
  287. (defconst makefile-font-lock-keywords (purecopy
  288.   (list
  289.    ;; Do macro assignments.  These get the "variable-name" face rather
  290.    ;; arbitrarily.
  291.    (list makefile-macroassign-regex 1 'font-lock-variable-name-face)
  292.    ;;
  293.    ;; Variable references even in targets/strings/comments:
  294.    '("\\$[({]\\([a-zA-Z0-9_]+\\)[})]" 1 font-lock-reference-face prepend)
  295.    ;;
  296.    ;; Do dependencies.  These get the function name face.
  297.    (list makefile-dependency-regex 1 'font-lock-function-name-face 'prepend)
  298.  
  299.    ;; Highlight lines that contain just whitespace.
  300.    ;; They can cause trouble, especially if they start with a tab.
  301.    '("^[ \t]+$" . makefile-space-face)
  302.  
  303.    ;; Highlight shell comments that Make treats as commands,
  304.    ;; since these can fool people.
  305.    '("^\t+#" 0 makefile-space-face t)
  306.  
  307.    ;; Highlight spaces that precede tabs.
  308.    ;; They can make a tab fail to be effective.
  309.    '("^\\( +\\)\t" 1 makefile-space-face)))
  310.   "Additional expressions to highlight in makefiles")
  311.  
  312. (put 'makefile-mode 'font-lock-defaults '(makefile-font-lock-keywords))
  313.  
  314. ;;; ------------------------------------------------------------
  315. ;;; The following configurable variables are used in the
  316. ;;; up-to-date overview .
  317. ;;; The standard configuration assumes that your `make' program
  318. ;;; can be run in question/query mode using the `-q' option, this
  319. ;;; means that the command
  320. ;;;
  321. ;;;    make -q foo
  322. ;;;
  323. ;;; should return an exit status of zero if the target `foo' is
  324. ;;; up to date and a nonzero exit status otherwise.
  325. ;;; Many makes can do this although the docs/manpages do not mention
  326. ;;; it. Try it with your favourite one.  GNU make, System V make, and
  327. ;;; Dennis Vadura's DMake have no problems.
  328. ;;; Set the variable `makefile-brave-make' to the name of the
  329. ;;; make utility that does this on your system.
  330. ;;; To understand what this is all about see the function definition
  331. ;;; of `makefile-query-by-make-minus-q' .
  332. ;;; ------------------------------------------------------------
  333.  
  334. (defcustom makefile-brave-make "make"
  335.   "A make that can handle the `-q' option."
  336.   :type 'string
  337.   :group 'makefile-mode)
  338.  
  339. (defcustom makefile-query-one-target-method 'makefile-query-by-make-minus-q
  340.   "Function to call to determine whether a make target is up to date.
  341. The function must satisfy this calling convention:
  342.  
  343. * As its first argument, it must accept the name of the target to
  344.   be checked, as a string.
  345.  
  346. * As its second argument, it may accept the name of a makefile
  347.   as a string. Depending on what you're going to do you may
  348.   not need this.
  349.  
  350. * It must return the integer value 0 (zero) if the given target
  351.   should be considered up-to-date in the context of the given
  352.   makefile, any nonzero integer value otherwise."
  353.   :type 'function
  354.   :group 'makefile-mode)
  355.  
  356. (defcustom makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*"
  357.   "Name of the Up-to-date overview buffer."
  358.   :type 'string
  359.   :group 'makefile-mode)
  360.  
  361. ;;; --- end of up-to-date-overview configuration ------------------
  362.  
  363. (defvar makefile-mode-map nil
  364.   "The keymap that is used in Makefile mode.")
  365.  
  366. (if makefile-mode-map
  367.     ()
  368.   (setq makefile-mode-map (make-sparse-keymap 'makefile-mode-map))
  369.   ;; set up the keymap
  370.   (define-key makefile-mode-map "\C-c:" 'makefile-insert-target-ref)
  371.   (if makefile-electric-keys
  372.       (progn
  373.     (define-key makefile-mode-map "$" 'makefile-insert-macro-ref)
  374.     (define-key makefile-mode-map ":" 'makefile-electric-colon)
  375.     (define-key makefile-mode-map "=" 'makefile-electric-equal)
  376.     (define-key makefile-mode-map "." 'makefile-electric-dot)))
  377.   (define-key makefile-mode-map "\C-c\C-f" 'makefile-pickup-filenames-as-targets)
  378.   (define-key makefile-mode-map "\C-c\C-b" 'makefile-switch-to-browser)
  379.   (define-key makefile-mode-map "\C-c\C-p" 'makefile-pickup-everything)
  380.   (define-key makefile-mode-map "\C-c\C-u" 'makefile-create-up-to-date-overview)
  381.   (define-key makefile-mode-map "\C-c\C-i" 'makefile-insert-gmake-function)
  382.   (define-key makefile-mode-map "\C-c\C-\\" 'makefile-backslash-region)
  383.   (define-key makefile-mode-map "\M-p"     'makefile-previous-dependency)
  384.   (define-key makefile-mode-map "\M-n"     'makefile-next-dependency)
  385.   (define-key makefile-mode-map "\e\t"     'makefile-complete))
  386.  
  387. ;; XEmacs change
  388. (defconst makefile-menubar-menu
  389.   (purecopy
  390.    '("Makefile"
  391.      ["Move to Next Dependency" makefile-next-dependency t]
  392.      ["Move to Previous Dependency" makefile-previous-dependency t]
  393.      "---"
  394.      ["Find Targets and Macros" makefile-pickup-everything t]
  395.      ["Complete Target or Macro" makefile-complete t]
  396.      ["Pop up Makefile Browser" makefile-switch-to-browser t])))
  397.  
  398. ;; XEmacs change
  399. (defconst makefile-popup-menu
  400.   (purecopy
  401.    (cons "Makefile Mode Commands"
  402.      (cdr makefile-menubar-menu))))
  403.  
  404. (defvar makefile-browser-map nil
  405.   "The keymap that is used in the macro- and target browser.")
  406. (if makefile-browser-map
  407.     ()
  408.   (setq makefile-browser-map (make-sparse-keymap))
  409.   (define-key makefile-browser-map "n"    'makefile-browser-next-line)
  410.   (define-key makefile-browser-map "\C-n" 'makefile-browser-next-line)    
  411.   (define-key makefile-browser-map "p"    'makefile-browser-previous-line)
  412.   (define-key makefile-browser-map "\C-p" 'makefile-browser-previous-line)
  413.   (define-key makefile-browser-map " "    'makefile-browser-toggle)
  414.   (define-key makefile-browser-map "i"    'makefile-browser-insert-selection)
  415.   (define-key makefile-browser-map "I"    'makefile-browser-insert-selection-and-quit)  
  416.   (define-key makefile-browser-map "\C-c\C-m" 'makefile-browser-insert-continuation)
  417.   (define-key makefile-browser-map "q"    'makefile-browser-quit)
  418.   ;; disable horizontal movement
  419.   (define-key makefile-browser-map "\C-b" 'undefined)
  420.   (define-key makefile-browser-map "\C-f" 'undefined))  
  421.  
  422.  
  423. (defvar makefile-mode-syntax-table nil)
  424. (if makefile-mode-syntax-table
  425.     ()
  426.   (setq makefile-mode-syntax-table (make-syntax-table))
  427.   (modify-syntax-entry ?\( "()    " makefile-mode-syntax-table)
  428.   (modify-syntax-entry ?\) ")(    " makefile-mode-syntax-table)
  429.   (modify-syntax-entry ?\[ "(]    " makefile-mode-syntax-table)
  430.   (modify-syntax-entry ?\] ")[    " makefile-mode-syntax-table)
  431.   (modify-syntax-entry ?\{ "(}    " makefile-mode-syntax-table)  
  432.   (modify-syntax-entry ?\} "){    " makefile-mode-syntax-table)
  433.   (modify-syntax-entry ?\' "\"     " makefile-mode-syntax-table)
  434.   (modify-syntax-entry ?\` "\"     " makefile-mode-syntax-table)
  435.   (modify-syntax-entry ?#  "<     " makefile-mode-syntax-table)
  436.   (modify-syntax-entry ?\n ">     " makefile-mode-syntax-table))
  437.  
  438.  
  439. ;;; ------------------------------------------------------------
  440. ;;; Internal variables.
  441. ;;; You don't need to configure below this line.
  442. ;;; ------------------------------------------------------------
  443.  
  444. (defvar makefile-target-table nil
  445.   "Table of all target names known for this buffer.")
  446.  
  447. (defvar makefile-macro-table nil
  448.   "Table of all macro names known for this buffer.")
  449.  
  450. (defvar makefile-browser-client
  451.   "A buffer in Makefile mode that is currently using the browser.")
  452.  
  453. (defvar makefile-browser-selection-vector nil)
  454. (defvar makefile-has-prereqs nil)
  455. (defvar makefile-need-target-pickup t)
  456. (defvar makefile-need-macro-pickup t)
  457.  
  458. (defvar makefile-mode-hook '())
  459.  
  460. ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
  461. ;; Each "ARG" is used as a prompt for a required argument.
  462. (defconst makefile-gnumake-functions-alist
  463.   '(
  464.     ;; Text functions
  465.     ("subst" "From" "To" "In")
  466.     ("patsubst" "Pattern" "Replacement" "In")
  467.     ("strip" "Text")
  468.     ("findstring" "Find what" "In")
  469.     ("filter" "Pattern" "Text")
  470.     ("filter-out" "Pattern" "Text")
  471.     ("sort" "List")
  472.     ;; Filename functions
  473.     ("dir" "Names")
  474.     ("notdir" "Names")
  475.     ("suffix" "Names")
  476.     ("basename" "Names")
  477.     ("addprefix" "Prefix" "Names")
  478.     ("addsuffix" "Suffix" "Names")
  479.     ("join" "List 1" "List 2")
  480.     ("word" "Index" "Text")
  481.     ("words" "Text")
  482.     ("firstword" "Text")
  483.     ("wildcard" "Pattern")
  484.     ;; Misc functions
  485.     ("foreach" "Variable" "List" "Text")
  486.     ("origin" "Variable")
  487.     ("shell" "Command")))
  488.  
  489.  
  490. ;;; ------------------------------------------------------------
  491. ;;; The mode function itself.
  492. ;;; ------------------------------------------------------------
  493.  
  494. ;;;###autoload
  495. (defun makefile-mode ()
  496.   "Major mode for editing Makefiles.
  497. This function ends by invoking the function(s) `makefile-mode-hook'.
  498.  
  499. \\{makefile-mode-map}
  500.  
  501. In the browser, use the following keys:
  502.  
  503. \\{makefile-browser-map}
  504.  
  505. Makefile mode can be configured by modifying the following variables:
  506.  
  507. makefile-browser-buffer-name:
  508.     Name of the macro- and target browser buffer.
  509.  
  510. makefile-target-colon:
  511.     The string that gets appended to all target names
  512.     inserted by `makefile-insert-target'.
  513.     \":\" or \"::\" are quite common values.
  514.  
  515. makefile-macro-assign:
  516.    The string that gets appended to all macro names
  517.    inserted by `makefile-insert-macro'.
  518.    The normal value should be \" = \", since this is what
  519.    standard make expects. However, newer makes such as dmake
  520.    allow a larger variety of different macro assignments, so you
  521.    might prefer to use \" += \" or \" := \" .
  522.  
  523. makefile-tab-after-target-colon:
  524.    If you want a TAB (instead of a space) to be appended after the
  525.    target colon, then set this to a non-nil value.
  526.  
  527. makefile-browser-leftmost-column:
  528.    Number of blanks to the left of the browser selection mark.
  529.  
  530. makefile-browser-cursor-column:
  531.    Column in which the cursor is positioned when it moves
  532.    up or down in the browser.
  533.  
  534. makefile-browser-selected-mark:
  535.    String used to mark selected entries in the browser.
  536.  
  537. makefile-browser-unselected-mark:
  538.    String used to mark unselected entries in the browser.
  539.  
  540. makefile-browser-auto-advance-after-selection-p:
  541.    If this variable is set to a non-nil value the cursor
  542.    will automagically advance to the next line after an item
  543.    has been selected in the browser.
  544.  
  545. makefile-pickup-everything-picks-up-filenames-p:
  546.    If this variable is set to a non-nil value then
  547.    `makefile-pickup-everything' also picks up filenames as targets
  548.    (i.e. it calls `makefile-find-filenames-as-targets'), otherwise
  549.    filenames are omitted.
  550.  
  551. makefile-cleanup-continuations-p:
  552.    If this variable is set to a non-nil value then makefile-mode
  553.    will assure that no line in the file ends with a backslash
  554.    (the continuation character) followed by any whitespace.
  555.    This is done by silently removing the trailing whitespace, leaving
  556.    the backslash itself intact.
  557.    IMPORTANT: Please note that enabling this option causes makefile-mode
  558.    to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
  559.  
  560. makefile-browser-hook:
  561.    A function or list of functions to be called just before the
  562.    browser is entered. This is executed in the makefile buffer.
  563.  
  564. makefile-special-targets-list:
  565.    List of special targets. You will be offered to complete
  566.    on one of those in the minibuffer whenever you enter a `.'.
  567.    at the beginning of a line in Makefile mode."
  568.  
  569.   (interactive)
  570.   (kill-all-local-variables)
  571.   (make-local-variable 'local-write-file-hooks)
  572.   (setq local-write-file-hooks
  573.     '(makefile-cleanup-continuations makefile-warn-suspicious-lines))
  574.   (make-local-variable 'makefile-target-table)
  575.   (make-local-variable 'makefile-macro-table)
  576.   (make-local-variable 'makefile-has-prereqs)
  577.   (make-local-variable 'makefile-need-target-pickup)
  578.   (make-local-variable 'makefile-need-macro-pickup)
  579.  
  580.   ;; Font lock.
  581.   (make-local-variable 'font-lock-defaults)
  582.   (setq font-lock-defaults '(makefile-font-lock-keywords))
  583.  
  584.   ;; Add-log.
  585.   (make-local-variable 'add-log-current-defun-function)
  586.   (setq add-log-current-defun-function 'makefile-add-log-defun)
  587.  
  588.   ;; Imenu.
  589.   (unless (featurep 'xemacs)
  590.     (make-local-variable 'imenu-create-index-function)
  591.     (setq imenu-create-index-function 'makefile-menu-index-function))
  592.  
  593.   ;; Dabbrev.
  594.   (make-local-variable 'dabbrev-abbrev-skip-leading-regexp)
  595.   (setq dabbrev-abbrev-skip-leading-regexp "\\$")
  596.  
  597.   ;; Comment stuff.
  598.   (make-local-variable 'comment-start)
  599.   (setq comment-start "#")
  600.   (make-local-variable 'comment-end)
  601.   (setq comment-end "")
  602.   (make-local-variable 'comment-start-skip)
  603.   (setq comment-start-skip "#+[ \t]*")
  604.  
  605.   ;; become the current major mode
  606.   (setq major-mode 'makefile-mode)
  607.   (setq mode-name "Makefile")
  608.  
  609.   ;; Activate keymap and syntax table.
  610.   (use-local-map makefile-mode-map)
  611.   (set-syntax-table makefile-mode-syntax-table)
  612.  
  613.   ;; Set menu
  614.   ;; XEmacs addition
  615.   (setq mode-popup-menu makefile-popup-menu)
  616.   (if (featurep 'menubar)
  617.       (progn
  618.     ;; make a local copy of the menubar, so our modes don't
  619.     ;; change the global menubar
  620.     (set-buffer-menubar current-menubar)
  621.     (add-submenu nil makefile-menubar-menu)))
  622.  
  623.   ;; Real TABs are important in makefiles
  624.   (setq indent-tabs-mode t)
  625.   (run-hooks 'makefile-mode-hook))
  626.  
  627.  
  628.  
  629. ;;; Motion code.
  630.  
  631. (defun makefile-next-dependency ()
  632.   "Move point to the beginning of the next dependency line."
  633.   (interactive)
  634.   (let ((here (point)))
  635.     (end-of-line)
  636.     (if (re-search-forward makefile-dependency-regex (point-max) t)
  637.     (progn (beginning-of-line) t)    ; indicate success
  638.       (goto-char here) nil)))
  639.  
  640. (defun makefile-previous-dependency ()
  641.   "Move point to the beginning of the previous dependency line."
  642.   (interactive)
  643.   (let ((here (point)))
  644.     (beginning-of-line)
  645.     (if (re-search-backward makefile-dependency-regex (point-min) t)
  646.     (progn (beginning-of-line) t)    ; indicate success
  647.       (goto-char here) nil)))
  648.  
  649.  
  650.  
  651. ;;; Electric keys.  Blech.
  652.  
  653. (defun makefile-electric-dot (arg)
  654.   "Prompt for the name of a special target to insert.
  655. Only does electric insertion at beginning of line.
  656. Anywhere else just self-inserts."
  657.   (interactive "p")
  658.   (if (bolp)
  659.       (makefile-insert-special-target)
  660.     (self-insert-command arg)))
  661.  
  662. (defun makefile-insert-special-target ()
  663.   "Propmt for and insert a special target name.
  664. Uses `makefile-special-targets' list."
  665.   (interactive)
  666.   (makefile-pickup-targets)
  667.   (let ((special-target
  668.      (completing-read "Special target: "
  669.               makefile-special-targets-list nil nil nil)))
  670.     (if (zerop (length special-target))
  671.     ()
  672.       (insert "." special-target ":")
  673.       (makefile-forward-after-target-colon))))
  674.  
  675. (defun makefile-electric-equal (arg)
  676.   "Prompt for name of a macro to insert.
  677. Only does prompting if point is at beginning of line.
  678. Anywhere else just self-inserts."
  679.   (interactive "p")
  680.   (makefile-pickup-macros)
  681.   (if (bolp)
  682.       (call-interactively 'makefile-insert-macro)
  683.     (self-insert-command arg)
  684.     ;; from here down is new -- if they inserted a macro without using
  685.     ;; the electric behavior, pick it up anyway   -gk
  686.     (save-excursion
  687.       (beginning-of-line)
  688.       (if (looking-at makefile-macroassign-regex)
  689.           (makefile-add-this-line-macro)))))
  690.  
  691. (defun makefile-insert-macro (macro-name)
  692.   "Prepare definition of a new macro."
  693.   (interactive "sMacro Name: ")
  694.   (makefile-pickup-macros)
  695.   (if (not (zerop (length macro-name)))
  696.       (progn
  697.     (beginning-of-line)
  698.     (insert macro-name makefile-macro-assign)
  699.     (setq makefile-need-macro-pickup t)
  700.     (makefile-remember-macro macro-name))))
  701.  
  702. (defun makefile-insert-macro-ref (macro-name)
  703.   "Complete on a list of known macros, then insert complete ref at point."
  704.   (interactive
  705.    (list
  706.     (progn
  707.       (makefile-pickup-macros)
  708.       (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
  709.   (makefile-do-macro-insertion macro-name))
  710.  
  711. (defun makefile-insert-target (target-name)
  712.   "Prepare definition of a new target (dependency line)."
  713.   (interactive "sTarget: ")
  714.   (if (not (zerop (length target-name)))
  715.       (progn
  716.     (beginning-of-line)
  717.     (insert target-name makefile-target-colon)
  718.     (makefile-forward-after-target-colon)
  719.     (end-of-line)
  720.     (setq makefile-need-target-pickup t)
  721.     (makefile-remember-target target-name))))
  722.  
  723. (defun makefile-insert-target-ref (target-name)
  724.   "Complete on a list of known targets, then insert target-ref at point."
  725.   (interactive
  726.    (list
  727.     (progn
  728.      (makefile-pickup-targets)
  729.      (completing-read "Refer to target: " makefile-target-table nil nil nil))))
  730.    (if (not (zerop (length target-name)))
  731.        (insert target-name " ")))
  732.  
  733. (defun makefile-electric-colon (arg)
  734.   "Prompt for name of new target.
  735. Prompting only happens at beginning of line.
  736. Anywhere else just self-inserts."
  737.   (interactive "p")
  738.   (if (bolp)
  739.       (call-interactively 'makefile-insert-target)
  740.     (self-insert-command arg)))
  741.  
  742.  
  743.  
  744. ;;; ------------------------------------------------------------
  745. ;;; Extracting targets and macros from an existing makefile
  746. ;;; ------------------------------------------------------------
  747.  
  748. (defun makefile-pickup-targets ()
  749.   "Notice names of all target definitions in Makefile."
  750.   (interactive)
  751.   (if (not makefile-need-target-pickup)
  752.       nil
  753.     (setq makefile-need-target-pickup nil)
  754.     (setq makefile-target-table nil)
  755.     (setq makefile-has-prereqs nil)
  756.     (save-excursion
  757.       (goto-char (point-min))
  758.       (while (re-search-forward makefile-dependency-regex (point-max) t)
  759.     (makefile-add-this-line-targets)))
  760.     (message "Read targets OK.")))
  761.  
  762. (defun makefile-add-this-line-targets ()
  763.   (save-excursion
  764.     (beginning-of-line)
  765.     (let ((done-with-line nil)
  766.       (line-number (1+ (count-lines (point-min) (point)))))
  767.       (while (not done-with-line)
  768.     (skip-chars-forward " \t")
  769.     (if (not (setq done-with-line (or (eolp)
  770.                       (char-equal (char-after (point)) ?:))))
  771.         (progn
  772.           (let* ((start-of-target-name (point))
  773.              (target-name
  774.               (progn
  775.             (skip-chars-forward "^ \t:#")
  776.             (buffer-substring start-of-target-name (point))))
  777.              (has-prereqs
  778.               (not (looking-at ":[ \t]*$"))))
  779.         (if (makefile-remember-target target-name has-prereqs)
  780.             (message "Picked up target \"%s\" from line %d"
  781.                  target-name line-number)))))))))
  782.  
  783. (defun makefile-pickup-macros ()
  784.   "Notice names of all macro definitions in Makefile."
  785.   (interactive)
  786.   (if (not makefile-need-macro-pickup)
  787.       nil
  788.     (setq makefile-need-macro-pickup nil)
  789.     ;; changed the nil in the next line to makefile-runtime-macros-list
  790.     ;; so you don't have to confirm on every runtime macro entered...  -gk
  791.     (setq makefile-macro-table makefile-runtime-macros-list) 
  792.     (save-excursion
  793.       (goto-char (point-min))
  794.       (while (re-search-forward makefile-macroassign-regex (point-max) t)
  795.     (makefile-add-this-line-macro)
  796.     (forward-line 1)))
  797.     (message "Read macros OK.")))
  798.  
  799. (defun makefile-add-this-line-macro ()
  800.   (save-excursion
  801.     (beginning-of-line)
  802.     (skip-chars-forward " \t")
  803.     (if (not (eolp))
  804.     (let* ((start-of-macro-name (point))
  805.            (line-number (1+ (count-lines (point-min) (point))))
  806.            (macro-name (progn
  807.                  (skip-chars-forward "^ \t:#=*")
  808.                  (buffer-substring start-of-macro-name (point)))))
  809.       (if (makefile-remember-macro macro-name)
  810.           (message "Picked up macro \"%s\" from line %d"
  811.                macro-name line-number))))))
  812.  
  813. (defun makefile-pickup-everything (arg)
  814.   "Notice names of all macros and targets in Makefile.
  815. Prefix arg means force pickups to be redone."
  816.   (interactive "P")
  817.   (if arg
  818.       (progn
  819.     (setq makefile-need-target-pickup t)
  820.     (setq makefile-need-macro-pickup t)))
  821.   (makefile-pickup-macros)
  822.   (makefile-pickup-targets)
  823.   (if makefile-pickup-everything-picks-up-filenames-p
  824.       (makefile-pickup-filenames-as-targets)))
  825.  
  826. (defun makefile-pickup-filenames-as-targets ()
  827.   "Scan the current directory for filenames to use as targets.
  828. Checks each filename against `makefile-ignored-files-in-pickup-regex'
  829. and adds all qualifying names to the list of known targets."
  830.   (interactive)
  831.   (let* ((dir (file-name-directory (buffer-file-name)))
  832.      (raw-filename-list (if dir
  833.                 (file-name-all-completions "" dir)
  834.                   (file-name-all-completions "" ""))))
  835.     (mapcar '(lambda (name)
  836.            (if (and (not (file-directory-p name))
  837.             (not (string-match makefile-ignored-files-in-pickup-regex
  838.                        name)))
  839.            (if (makefile-remember-target name)
  840.                (message "Picked up file \"%s\" as target" name))))
  841.         raw-filename-list)))
  842.  
  843.  
  844.  
  845. ;;; Completion.
  846.  
  847. (defun makefile-complete ()
  848.   "Perform completion on Makefile construct preceding point.
  849. Can complete variable and target names.
  850. The context determines which are considered."
  851.   (interactive)
  852.   (let* ((beg (save-excursion
  853.         (skip-chars-backward "^$(){}:#= \t\n")
  854.         (point)))
  855.      (try (buffer-substring beg (point)))
  856.      (do-macros nil)
  857.      (paren nil))
  858.  
  859.     (save-excursion
  860.       (goto-char beg)
  861.       (let ((pc (preceding-char)))
  862.     (cond
  863.      ;; Beginning of line means anything.
  864.      ((bolp)
  865.       ())
  866.  
  867.      ;; Preceding "$" means macros only.
  868.      ((= pc ?$)
  869.       (setq do-macros t))
  870.  
  871.      ;; Preceding "$(" or "${" means macros only.
  872.      ((and (or (= pc ?{)
  873.            (= pc ?\())
  874.            (progn
  875.          (setq paren pc)
  876.          (backward-char)
  877.          (and (not (bolp))
  878.               (= (preceding-char) ?$))))
  879.       (setq do-macros t)))))
  880.  
  881.     ;; Try completion.
  882.     (let* ((table (append (if do-macros
  883.                   '()
  884.                 makefile-target-table)
  885.               makefile-macro-table))
  886.        (completion (try-completion try table)))
  887.       (cond
  888.        ;; Exact match, so insert closing paren or colon.
  889.        ((eq completion t)
  890.     (insert (if do-macros
  891.             (if (eq paren ?{)
  892.             ?}
  893.               ?\))
  894.           (if (save-excursion
  895.             (goto-char beg)
  896.             (bolp))
  897.               ":"
  898.             " "))))
  899.  
  900.        ;; No match.
  901.        ((null completion)
  902.     (message "Can't find completion for \"%s\"" try)
  903.     (ding))
  904.  
  905.        ;; Partial completion.
  906.        ((not (string= try completion))
  907.     ;; FIXME it would be nice to supply the closing paren if an
  908.     ;; exact, unambiguous match were found.  That is not possible
  909.     ;; right now.  Ditto closing ":" for targets.
  910.     (delete-region beg (point))
  911.  
  912.     ;; DO-MACROS means doing macros only.  If not that, then check
  913.     ;; to see if this completion is a macro.  Special insertion
  914.     ;; must be done for macros.
  915.     (if (or do-macros
  916.         (assoc completion makefile-macro-table))
  917.         (let ((makefile-use-curly-braces-for-macros-p
  918.            (or (eq paren ?{)
  919.                makefile-use-curly-braces-for-macros-p)))
  920.           (delete-backward-char 2)
  921.           (makefile-do-macro-insertion completion)
  922.           (delete-backward-char 1))
  923.  
  924.       ;; Just insert targets.
  925.       (insert completion)))
  926.  
  927.        ;; Can't complete any more, so make completion list.  FIXME
  928.        ;; this doesn't do the right thing when the completion is
  929.        ;; actually inserted.  I don't think there is an easy way to do
  930.        ;; that.
  931.        (t
  932.     (message "Making completion list...")
  933.     (let ((list (all-completions try table)))
  934.       (with-output-to-temp-buffer "*Completions*"
  935.         (display-completion-list list)))
  936.     (message "Making completion list...done"))))))
  937.  
  938.  
  939.  
  940. ;; Backslashification.  Stolen from cc-mode.el.
  941.  
  942. (defun makefile-backslashify-current-line (doit)
  943.   (end-of-line)
  944.   (if doit
  945.       (if (not (save-excursion
  946.          (forward-char -1)
  947.          (eq (char-after (point)) ?\\ )))
  948.       (progn
  949.         (if (>= (current-column) makefile-backslash-column)
  950.         (insert " \\")
  951.           (while (<= (current-column) makefile-backslash-column)
  952.         (insert "\t")
  953.         (end-of-line))
  954.           (delete-char -1)
  955.           (while (< (current-column) makefile-backslash-column)
  956.         (insert " ")
  957.         (end-of-line))
  958.           (insert "\\"))))
  959.     (if (not (bolp))
  960.     (progn
  961.       (forward-char -1)
  962.       (if (eq (char-after (point)) ?\\ )
  963.           (let ((saved (save-excursion
  964.                 (end-of-line)
  965.                 (point))))
  966.         (skip-chars-backward " \t")
  967.         (delete-region (point) saved)))))))
  968.  
  969. (defun makefile-backslash-region (beg end arg)
  970.   "Insert backslashes at end of every line in region.
  971. Useful for defining multi-line rules.
  972. If called with a prefix argument, trailing backslashes are removed."
  973.   (interactive "r\nP")
  974.   (save-excursion
  975.     (let ((do-lastline-p (progn (goto-char end) (not (bolp)))))
  976.       (save-restriction
  977.     (narrow-to-region beg end)
  978.     (goto-char (point-min))
  979.     (while (not (save-excursion
  980.               (forward-line 1)
  981.               (eobp)))
  982.       (makefile-backslashify-current-line (null arg))
  983.       (forward-line 1)))
  984.       (and do-lastline-p
  985.        (progn (goto-char end)
  986.           (makefile-backslashify-current-line (null arg)))))))
  987.  
  988.  
  989.  
  990. ;;; ------------------------------------------------------------
  991. ;;; Browser mode.
  992. ;;; ------------------------------------------------------------
  993.  
  994. (defun makefile-browser-format-target-line (target selected)
  995.   (format
  996.    (concat (make-string makefile-browser-leftmost-column ?\ )
  997.        (if selected
  998.            makefile-browser-selected-mark
  999.          makefile-browser-unselected-mark)
  1000.        "%s%s")
  1001.    target makefile-target-colon))
  1002.  
  1003. (defun makefile-browser-format-macro-line (macro selected)
  1004.    (concat (make-string makefile-browser-leftmost-column ?\ )
  1005.        (if selected
  1006.            makefile-browser-selected-mark
  1007.          makefile-browser-unselected-mark)
  1008.        (makefile-format-macro-ref macro)))
  1009.  
  1010. (defun makefile-browser-fill (targets macros)
  1011.   (let ((inhibit-read-only t))
  1012.     (goto-char (point-min))
  1013.     (erase-buffer)
  1014.     (mapconcat
  1015.      (function
  1016.       (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n")))
  1017.      targets
  1018.      "")
  1019.     (mapconcat
  1020.      (function
  1021.       (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n")))
  1022.      macros
  1023.      "")
  1024.     (sort-lines nil (point-min) (point-max))
  1025.     (goto-char (1- (point-max)))
  1026.     (delete-char 1)            ; remove unnecessary newline at eob
  1027.     (goto-char (point-min))
  1028.     (forward-char makefile-browser-cursor-column)))
  1029.  
  1030. ;;;
  1031. ;;; Moving up and down in the browser
  1032. ;;;
  1033.  
  1034. (defun makefile-browser-next-line ()
  1035.   "Move the browser selection cursor to the next line."
  1036.   (interactive)
  1037.   (if (not (makefile-last-line-p))
  1038.       (progn
  1039.     (forward-line 1)
  1040.     (forward-char makefile-browser-cursor-column))))
  1041.  
  1042. (defun makefile-browser-previous-line ()
  1043.   "Move the browser selection cursor to the previous line."
  1044.   (interactive)
  1045.   (if (not (makefile-first-line-p))
  1046.       (progn
  1047.     (forward-line -1)
  1048.     (forward-char makefile-browser-cursor-column))))
  1049.  
  1050. ;;;
  1051. ;;; Quitting the browser (returns to client buffer)
  1052. ;;;
  1053.  
  1054. (defun makefile-browser-quit ()
  1055.   "Leave the browser and return to the makefile buffer."
  1056.   (interactive)
  1057.   (let ((my-client makefile-browser-client))
  1058.     (setq makefile-browser-client nil)    ; we quitted, so NO client!
  1059.     (set-buffer-modified-p nil)
  1060.     (kill-buffer (current-buffer))
  1061.     (pop-to-buffer my-client)))
  1062.  
  1063. ;;;
  1064. ;;; Toggle state of a browser item
  1065. ;;;
  1066.  
  1067. (defun makefile-browser-toggle ()
  1068.   "Toggle the selection state of the browser item at the cursor position."
  1069.   (interactive)
  1070.   (let ((this-line (count-lines (point-min) (point))))
  1071.     (setq this-line (max 1 this-line))
  1072.     (makefile-browser-toggle-state-for-line this-line)
  1073.     (goto-line this-line)
  1074.     (let ((inhibit-read-only t))
  1075.       (beginning-of-line)
  1076.       (if (makefile-browser-on-macro-line-p)
  1077.       (let ((macro-name (makefile-browser-this-line-macro-name)))
  1078.         (delete-region (point) (progn (end-of-line) (point)))
  1079.         (insert
  1080.          (makefile-browser-format-macro-line
  1081.         macro-name
  1082.         (makefile-browser-get-state-for-line this-line))))
  1083.     (let ((target-name (makefile-browser-this-line-target-name)))
  1084.       (delete-region (point) (progn (end-of-line) (point)))
  1085.       (insert
  1086.        (makefile-browser-format-target-line
  1087.           target-name
  1088.           (makefile-browser-get-state-for-line this-line))))))
  1089.     (beginning-of-line)
  1090.     (forward-char makefile-browser-cursor-column)
  1091.     (if makefile-browser-auto-advance-after-selection-p
  1092.     (makefile-browser-next-line))))
  1093.  
  1094. ;;;
  1095. ;;; Making insertions into the client buffer
  1096. ;;;
  1097.  
  1098. (defun makefile-browser-insert-continuation ()
  1099.   "Insert a makefile continuation.
  1100. In the makefile buffer, go to (end-of-line), insert a \'\\\'
  1101. character, insert a new blank line, go to that line and indent by one TAB.
  1102. This is most useful in the process of creating continued lines when copying
  1103. large dependencies from the browser to the client buffer.
  1104. \(point) advances accordingly in the client buffer."
  1105.   (interactive)
  1106.   (save-excursion
  1107.     (set-buffer makefile-browser-client)
  1108.     (end-of-line)
  1109.     (insert "\\\n\t")))
  1110.  
  1111. (defun makefile-browser-insert-selection ()
  1112.   "Insert all selected targets and/or macros in the makefile buffer.
  1113. Insertion takes place at point."
  1114.   (interactive)
  1115.   (save-excursion
  1116.     (goto-line 1)
  1117.     (let ((current-line 1))
  1118.       (while (not (eobp))
  1119.     (if (makefile-browser-get-state-for-line current-line)
  1120.         (makefile-browser-send-this-line-item))
  1121.     (forward-line 1)
  1122.     (setq current-line (1+ current-line))))))
  1123.  
  1124. (defun makefile-browser-insert-selection-and-quit ()
  1125.   (interactive)
  1126.   (makefile-browser-insert-selection)
  1127.   (makefile-browser-quit))
  1128.  
  1129. (defun makefile-browser-send-this-line-item ()
  1130.   (if (makefile-browser-on-macro-line-p)
  1131.       (save-excursion
  1132.     (let ((macro-name (makefile-browser-this-line-macro-name)))
  1133.       (set-buffer makefile-browser-client)
  1134.       (insert (makefile-format-macro-ref macro-name) " ")))
  1135.     (save-excursion
  1136.       (let ((target-name (makefile-browser-this-line-target-name)))
  1137.     (set-buffer makefile-browser-client)
  1138.     (insert target-name " ")))))
  1139.  
  1140. (defun makefile-browser-start-interaction ()
  1141.   (use-local-map makefile-browser-map)
  1142.   (setq buffer-read-only t))
  1143.  
  1144. (defun makefile-browse (targets macros)
  1145.   (if (zerop (+ (length targets) (length macros)))
  1146.       (progn
  1147.     (beep)
  1148.     (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
  1149.     (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name)))
  1150.     (pop-to-buffer browser-buffer)
  1151.     (make-variable-buffer-local 'makefile-browser-selection-vector)
  1152.     (makefile-browser-fill targets macros)
  1153.     (shrink-window-if-larger-than-buffer)
  1154.     (setq makefile-browser-selection-vector
  1155.           (make-vector (+ (length targets) (length macros)) nil))
  1156.     (makefile-browser-start-interaction))))
  1157.  
  1158. (defun makefile-switch-to-browser ()
  1159.   (interactive)
  1160.   (run-hooks 'makefile-browser-hook)
  1161.   (setq makefile-browser-client (current-buffer))
  1162.   (makefile-pickup-targets)
  1163.   (makefile-pickup-macros)
  1164.   (makefile-browse makefile-target-table
  1165.                    ;; take out the runtime macros which were added for completion sake -gk
  1166.                    (set-difference makefile-macro-table makefile-runtime-macros-list)))
  1167.  
  1168.  
  1169.  
  1170. ;;; ------------------------------------------------------------
  1171. ;;; Up-to-date overview buffer
  1172. ;;; ------------------------------------------------------------
  1173.  
  1174. (defun makefile-create-up-to-date-overview ()
  1175.   "Create a buffer containing an overview of the state of all known targets.
  1176. Known targets are targets that are explicitly defined in that makefile;
  1177. in other words, all targets that appear on the left hand side of a
  1178. dependency in the makefile."
  1179.   (interactive)
  1180.   (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
  1181.       ;;
  1182.       ;; The rest of this function operates on a temporary makefile, created by
  1183.       ;; writing the current contents of the makefile buffer.
  1184.       ;;
  1185.       (let ((saved-target-table makefile-target-table)
  1186.         (this-buffer (current-buffer))
  1187.         (makefile-up-to-date-buffer
  1188.          (get-buffer-create makefile-up-to-date-buffer-name))
  1189.         (filename (makefile-save-temporary))
  1190.         ;;
  1191.         ;; Forget the target table because it may contain picked-up filenames
  1192.         ;; that are not really targets in the current makefile.
  1193.         ;; We don't want to query these, so get a new target-table with just the
  1194.         ;; targets that can be found in the makefile buffer.
  1195.         ;; The 'old' target table will be restored later.
  1196.         ;;
  1197.         (real-targets (progn
  1198.                 (makefile-pickup-targets)
  1199.                 makefile-target-table))
  1200.         (prereqs makefile-has-prereqs)
  1201.         )
  1202.  
  1203.     (set-buffer makefile-up-to-date-buffer)
  1204.     (setq buffer-read-only nil)
  1205.     (erase-buffer)
  1206.     (makefile-query-targets filename real-targets prereqs)
  1207.     (if (zerop (buffer-size))        ; if it did not get us anything
  1208.         (progn
  1209.           (kill-buffer (current-buffer))
  1210.           (message "No overview created!")))
  1211.     (set-buffer this-buffer)
  1212.     (setq makefile-target-table saved-target-table)
  1213.     (if (get-buffer makefile-up-to-date-buffer-name)
  1214.         (progn
  1215.           (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name))
  1216.           (shrink-window-if-larger-than-buffer)
  1217.           (sort-lines nil (point-min) (point-max))
  1218.           (setq buffer-read-only t))))))
  1219.  
  1220. (defun makefile-save-temporary ()
  1221.   "Create a temporary file from the current makefile buffer."
  1222.   (let ((filename (makefile-generate-temporary-filename)))
  1223.     (write-region (point-min) (point-max) filename nil 0)
  1224.     filename))                ; return the filename
  1225.  
  1226. (defun makefile-generate-temporary-filename ()
  1227.   "Create a filename suitable for use in `makefile-save-temporary'.
  1228. Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
  1229. with the generated name!"
  1230.   (let ((my-name (user-login-name))
  1231.     (my-uid (int-to-string (user-uid))))
  1232.     (concat "mktmp"
  1233.       (if (> (length my-name) 3)
  1234.           (substring my-name 0 3)
  1235.         my-name)
  1236.       "."
  1237.       (if (> (length my-uid) 3)
  1238.           (substring my-uid 0 3)
  1239.         my-uid))))
  1240.  
  1241. (defun makefile-query-targets (filename target-table prereq-list)
  1242.   "Fill the up-to-date overview buffer.
  1243. Checks each target in TARGET-TABLE using `makefile-query-one-target-method'
  1244. and generates the overview, one line per target name."
  1245.   (insert
  1246.    (mapconcat
  1247.     (function (lambda (item)
  1248.         (let* ((target-name (car item))
  1249.                (no-prereqs (not (member target-name prereq-list)))
  1250.                (needs-rebuild (or no-prereqs 
  1251.                       (funcall
  1252.                        makefile-query-one-target-method
  1253.                        target-name
  1254.                        filename))))
  1255.           (format "\t%s%s"
  1256.               target-name
  1257.               (cond (no-prereqs "  .. has no prerequisites")
  1258.                 (needs-rebuild "  .. NEEDS REBUILD")
  1259.                 (t "  .. is up to date"))))
  1260.         ))
  1261.     target-table "\n"))
  1262.   (goto-char (point-min))
  1263.   (delete-file filename))        ; remove the tmpfile
  1264.  
  1265. (defun makefile-query-by-make-minus-q (target &optional filename)
  1266.   (not (zerop
  1267.     (call-process makefile-brave-make nil nil nil
  1268.               "-f" filename "-q" target))))
  1269.  
  1270.  
  1271.  
  1272. ;;; ------------------------------------------------------------
  1273. ;;; Continuation cleanup
  1274. ;;; ------------------------------------------------------------
  1275.  
  1276. (defun makefile-cleanup-continuations ()
  1277.   (if (eq major-mode 'makefile-mode)
  1278.       (if (and makefile-cleanup-continuations-p
  1279.            (not buffer-read-only))
  1280.       (save-excursion
  1281.         (goto-char (point-min))
  1282.         (while (re-search-forward "\\\\[ \t]+$" (point-max) t)
  1283.           (replace-match "\\" t t))))))
  1284.  
  1285.  
  1286. ;;; ------------------------------------------------------------
  1287. ;;; Warn of suspicious lines
  1288. ;;; ------------------------------------------------------------
  1289.  
  1290. (defun makefile-warn-suspicious-lines ()
  1291.   (let ((dont-save nil))
  1292.     (if (and (eq major-mode 'makefile-mode)
  1293.          makefile-warn-suspicious-lines-p)  ; -gk
  1294.     (let ((suspicious
  1295.            (save-excursion
  1296.          (goto-char (point-min))
  1297.          (re-search-forward
  1298.           "\\(^[\t]+$\\)\\|\\(^[ ]+[\t]\\)" (point-max) t))))
  1299.       (if suspicious
  1300.           (let ((line-nr (count-lines (point-min) suspicious)))
  1301.         (setq dont-save
  1302.               (not (y-or-n-p
  1303.                 (format "Suspicious line %d. Save anyway "
  1304.                     line-nr))))))))
  1305.     dont-save))
  1306.       
  1307.  
  1308.  
  1309. ;;; ------------------------------------------------------------
  1310. ;;; GNU make function support
  1311. ;;; ------------------------------------------------------------
  1312.  
  1313. (defun makefile-insert-gmake-function ()
  1314.   "Insert a GNU make function call.
  1315. Asks for the name of the function to use (with completion).
  1316. Then prompts for all required parameters."
  1317.   (interactive)
  1318.   (let* ((gm-function-name (completing-read
  1319.                  "Function: "
  1320.                  makefile-gnumake-functions-alist
  1321.                  nil t nil))
  1322.      (gm-function-prompts
  1323.       (cdr (assoc gm-function-name makefile-gnumake-functions-alist))))
  1324.     (if (not (zerop (length gm-function-name)))
  1325.     (insert (makefile-format-macro-ref
  1326.          (concat gm-function-name " "
  1327.              (makefile-prompt-for-gmake-funargs
  1328.                 gm-function-name gm-function-prompts)))
  1329.         " "))))
  1330.  
  1331. (defun makefile-prompt-for-gmake-funargs (function-name prompt-list)
  1332.   (mapconcat
  1333.    (function (lambda (one-prompt)
  1334.            (read-string (format "[%s] %s: " function-name one-prompt)
  1335.                 nil)))
  1336.    prompt-list
  1337.    ","))
  1338.  
  1339.  
  1340.  
  1341. ;;; ------------------------------------------------------------
  1342. ;;; Utility functions
  1343. ;;; ------------------------------------------------------------
  1344.  
  1345. (defun makefile-do-macro-insertion (macro-name)
  1346.   "Insert a macro reference."
  1347.   (if (not (zerop (length macro-name)))
  1348.       (if (assoc macro-name makefile-runtime-macros-list)
  1349.       (insert "$" macro-name)
  1350.     (insert (makefile-format-macro-ref macro-name)))))
  1351.  
  1352. (defun makefile-remember-target (target-name &optional has-prereqs)
  1353.   "Remember a given target if it is not already remembered for this buffer."
  1354.   (if (not (zerop (length target-name)))
  1355.       (progn
  1356.       (if (not (assoc target-name makefile-target-table))
  1357.       (setq makefile-target-table
  1358.         (cons (list target-name) makefile-target-table)))
  1359.       (if has-prereqs
  1360.       (setq makefile-has-prereqs
  1361.         (cons target-name makefile-has-prereqs))))))
  1362.  
  1363. (defun makefile-remember-macro (macro-name)
  1364.   "Remember a given macro if it is not already remembered for this buffer."
  1365.   (if (not (zerop (length macro-name)))
  1366.       (if (not (assoc macro-name makefile-macro-table))
  1367.       (setq makefile-macro-table
  1368.         (cons (list macro-name) makefile-macro-table)))))
  1369.  
  1370. (defun makefile-forward-after-target-colon ()
  1371.   "Move point forward after inserting the terminating colon of a target.
  1372. This acts according to the value of `makefile-tab-after-target-colon'."
  1373.   (if makefile-tab-after-target-colon
  1374.       (insert "\t")
  1375.     (insert " ")))
  1376.  
  1377. (defun makefile-browser-on-macro-line-p ()
  1378.   "Determine if point is on a macro line in the browser."
  1379.   (save-excursion
  1380.     (beginning-of-line)
  1381.     (re-search-forward "\\$[{(]" (makefile-end-of-line-point) t)))
  1382.  
  1383. (defun makefile-browser-this-line-target-name ()
  1384.   "Extract the target name from a line in the browser."
  1385.   (save-excursion
  1386.     (end-of-line)
  1387.     (skip-chars-backward "^ \t")
  1388.     (buffer-substring (point) (1- (makefile-end-of-line-point)))))
  1389.  
  1390. (defun makefile-browser-this-line-macro-name ()
  1391.   "Extract the macro name from a line in the browser."
  1392.   (save-excursion
  1393.     (beginning-of-line)
  1394.     (re-search-forward "\\$[{(]" (makefile-end-of-line-point) t)
  1395.     (let ((macro-start (point)))
  1396.       (skip-chars-forward "^})")
  1397.       (buffer-substring macro-start (point)))))
  1398.  
  1399. (defun makefile-format-macro-ref (macro-name)
  1400.   "Format a macro reference.
  1401. Uses `makefile-use-curly-braces-for-macros-p'."
  1402.   (if (or (char-equal ?\( (string-to-char macro-name))
  1403.       (char-equal ?\{ (string-to-char macro-name)))
  1404.       (format "$%s" macro-name)
  1405.     (if makefile-use-curly-braces-for-macros-p
  1406.     (format "${%s}" macro-name)
  1407.       (format "$(%s)" macro-name))))
  1408.  
  1409. (defun makefile-browser-get-state-for-line (n)
  1410.   (aref makefile-browser-selection-vector (1- n)))
  1411.  
  1412. (defun makefile-browser-set-state-for-line (n to-state)
  1413.   (aset makefile-browser-selection-vector (1- n) to-state))
  1414.  
  1415. (defun makefile-browser-toggle-state-for-line (n)
  1416.   (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n))))
  1417.  
  1418. (defun makefile-beginning-of-line-point ()
  1419.   (save-excursion
  1420.     (beginning-of-line)
  1421.     (point)))
  1422.  
  1423. (defun makefile-end-of-line-point ()
  1424.   (save-excursion
  1425.     (end-of-line)
  1426.     (point)))
  1427.  
  1428. (defun makefile-last-line-p ()
  1429.   (= (makefile-end-of-line-point) (point-max)))
  1430.  
  1431. (defun makefile-first-line-p ()
  1432.   (= (makefile-beginning-of-line-point) (point-min)))
  1433.  
  1434.  
  1435.  
  1436. ;;; Support for other packages, like add-log and imenu.
  1437.  
  1438. (defun makefile-add-log-defun ()
  1439.   "Return name of target or variable assignment that point is in.
  1440. If it isn't in one, return nil."
  1441.   (save-excursion
  1442.     (let (found)
  1443.       (beginning-of-line)
  1444.       ;; Scan back line by line, noticing when we come to a
  1445.       ;; variable or rule definition, and giving up when we see
  1446.       ;; a line that is not part of either of those.
  1447.       (while (not found)
  1448.     (cond
  1449.      ((looking-at makefile-macroassign-regex)
  1450.       (setq found (buffer-substring-no-properties (match-beginning 1)
  1451.                               (match-end 1))))
  1452.      ((looking-at makefile-dependency-regex)
  1453.       (setq found (buffer-substring-no-properties (match-beginning 1)
  1454.                               (match-end 1))))
  1455.      ;; Don't keep looking across a blank line or comment.  Give up.
  1456.      ((looking-at "$\\|#")
  1457.       (setq found 'bobp))
  1458.      ((bobp)
  1459.       (setq found 'bobp)))
  1460.     (or found
  1461.         (forward-line -1)))
  1462.       (if (stringp found) found))))
  1463.  
  1464. ;; FIXME it might be nice to have them separated by macro vs target.
  1465. (defun makefile-menu-index-function ()
  1466.   ;; "Generate alist of indices for imenu."
  1467.   (let (alist
  1468.     stupid
  1469.     (re (concat makefile-dependency-regex
  1470.             "\\|"
  1471.             makefile-macroassign-regex)))
  1472.     (imenu-progress-message stupid 0)
  1473.     (goto-char (point-min))
  1474.     (while (re-search-forward re nil t)
  1475.       (imenu-progress-message stupid)
  1476.       (let ((n (if (match-beginning 1) 1 5)))
  1477.     (setq alist (cons
  1478.              (cons (buffer-substring (match-beginning n)
  1479.                          (match-end n))
  1480.                (match-beginning n))
  1481.              alist))))
  1482.     (imenu-progress-message stupid 100)
  1483.     (nreverse alist)))
  1484.  
  1485. ;;; make-mode.el ends here
  1486.